﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Booking_Hotel.BLL;
using Booking_Hotel.DBML;
using System.Web.Security;
using Booking_Hotel.DAL;

namespace Booking_Hotel
{
    public partial class Supplier_KhuyenMai : System.Web.UI.Page
    {
        ThanhBHotelDataContext tbk = new ThanhBHotelDataContext();
        Supplier_Hotel_KhuyenMai entities = new Supplier_Hotel_KhuyenMai();
        int KhuyenMaiID;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["ss_SupplierID"] = 2;//Gan SupplierID cho Dropdownlist de lay DS khach san cua Supplier
                ShowKhuyenMai();
            }
        }
        protected void ShowKhuyenMai()
        {
            var khuyenmai = from dv in tbk.Supplier_Hotels
                        join shk in tbk.Supplier_Hotel_KhuyenMais on dv.Supplier_HotelID equals shk.Supplier_HotelID
                        where dv.SupplierID == (int)Session["ss_SupplierID"]
                        select new
                        {
                            Supplier_Hotel_KhuyenMaiID = shk.Supplier_Hotel_KhuyenMaiID,
                            Supplier_HotelID = shk.Supplier_HotelID,
                            TenKhachSan = dv.TenKhachSan,
                            TieuDeKhuyenMai = shk.TieuDeKhuyenMai,
                            NoiDungKhuyenmai = shk.NoiDungKhuyenmai,
                            NgayBatDauDang= shk.NgayBatDauDang,
                            NgayKetThucDang= shk.NgayKetThucDang
                        };
            GridView1.DataSource = khuyenmai;
            GridView1.DataBind();

        }

        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.Cells.Count > 0)
            {
                e.Row.Cells[0].Visible = false;
                e.Row.Cells[1].Visible = false;
            }
        }

        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            GridViewRow select = GridView1.Rows[e.NewSelectedIndex];
            Session["ss_Supplier_Hotel_KhuyenMaiID"] = int.Parse(select.Cells[0].Text.Trim());
            DDL_TenKhachSan.SelectedValue = select.Cells[1].Text.Trim();
            txt_TieuDeKhuyenMai.Text = select.Cells[3].Text.Trim();
            txt_NoiDungKhuyenMai.Text = select.Cells[4].Text.Trim();
            txt_NgayBatDauDang.Text = select.Cells[5].Text.Trim();
            txt_NgayKetThucDang.Text = select.Cells[6].Text.Trim();
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton bt_Delete = sender as ImageButton;
            GridViewRow row = (GridViewRow)bt_Delete.NamingContainer;
            KhuyenMaiID = int.Parse(row.Cells[0].Text.Trim());
            Supplier_Hotel_KhuyenMai shk = tbk.Supplier_Hotel_KhuyenMais.Where(c => c.Supplier_Hotel_KhuyenMaiID == KhuyenMaiID).Single();
            tbk.Supplier_Hotel_KhuyenMais.DeleteOnSubmit(shk);
            tbk.SubmitChanges();
            ShowKhuyenMai();
        }

        protected void btn_Insert_Click(object sender, EventArgs e)
        {
            try
            {
                entities = new Supplier_Hotel_KhuyenMai();
                entities.Supplier_HotelID = int.Parse(DDL_TenKhachSan.SelectedValue);
                entities.TieuDeKhuyenMai = txt_TieuDeKhuyenMai.Text.Trim();
                entities.NoiDungKhuyenmai = txt_NoiDungKhuyenMai.Text.Trim();
                DateTime dtime = DateTime.Parse(txt_NgayBatDauDang.Text);
                entities.NgayBatDauDang = dtime;
                DateTime dtime1 = DateTime.Parse(txt_NgayKetThucDang.Text);
                entities.NgayKetThucDang = dtime1;
                Supplier_KhuyenMaiDAO sk = new Supplier_KhuyenMaiDAO();
                sk.addSupplier_Hotel_KhuyenMai(entities);
                ShowKhuyenMai();

            }
            catch (Exception)
            {

                throw;
            }
        }

        protected void btn_Update_Click(object sender, EventArgs e)
        {
            try
            {
                int check_Supplier_Hotel_KhuyenMaiID = (int)(Session["ss_Supplier_Hotel_KhuyenMaiID"]);
                if (check_Supplier_Hotel_KhuyenMaiID != null)
                {
                    entities = new Supplier_Hotel_KhuyenMai();
                    entities.Supplier_Hotel_KhuyenMaiID = check_Supplier_Hotel_KhuyenMaiID;
                    entities.Supplier_HotelID = int.Parse(DDL_TenKhachSan.SelectedValue);
                    entities.TieuDeKhuyenMai = txt_TieuDeKhuyenMai.Text.Trim();
                    entities.NoiDungKhuyenmai = txt_NoiDungKhuyenMai.Text.Trim();
                    DateTime dtime = DateTime.Parse(txt_NgayBatDauDang.Text);
                    entities.NgayBatDauDang = dtime;
                    DateTime dtime1 = DateTime.Parse(txt_NgayKetThucDang.Text);
                    entities.NgayKetThucDang = dtime1;
                    Supplier_KhuyenMaiDAO sk = new Supplier_KhuyenMaiDAO();
                    sk.updateSupplier_Hotel_KhuyenMai(entities);
                    ShowKhuyenMai();
                }
                else
                {
                    MyInclude.MessageBox.ShowMessage("Vui lòng chọn đối tượng cần update dưới lưới", this.Page);

                }

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}